from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime

class ReleaseBase(BaseModel):
    """版本发布基础模式"""
    project_name: str = Field(..., max_length=100, description="项目名称")
    version: Optional[str] = Field(None, max_length=50, description="版本号")
    environment: str = Field(..., max_length=50, description="部署环境")
    branch: Optional[str] = Field(None, max_length=100, description="分支名称")
    commit_hash: Optional[str] = Field(None, max_length=100, description="提交哈希")
    description: Optional[str] = Field(None, description="发布描述")
    release_notes: Optional[str] = Field(None, description="发布说明")

class ReleaseCreate(ReleaseBase):
    """创建版本发布模式"""
    strategy: Optional[str] = Field("rolling", description="部署策略")
    rollback_version: Optional[str] = Field(None, description="回滚版本")
    auto_deploy: bool = Field(False, description="是否自动部署")

class ReleaseUpdate(BaseModel):
    """更新版本发布模式"""
    description: Optional[str] = Field(None, description="发布描述")
    release_notes: Optional[str] = Field(None, description="发布说明")
    status: Optional[str] = Field(None, description="发布状态")

class Release(ReleaseBase):
    """版本发布响应模式"""
    id: int
    release_id: str
    status: str
    strategy: Optional[str] = None
    rollback_version: Optional[str] = None
    created_by: int
    deployed_by: Optional[int] = None
    created_at: datetime
    updated_at: Optional[datetime]
    deployed_at: Optional[datetime]
    
    class Config:
        from_attributes = True

class ReleaseLogBase(BaseModel):
    """发布日志基础模式"""
    action: str = Field(..., description="操作类型")
    message: str = Field(..., description="日志消息")
    status: str = Field(..., description="日志状态")

class ReleaseLog(ReleaseLogBase):
    """发布日志响应模式"""
    id: int
    release_id: int
    user_id: Optional[int] = None
    progress: Optional[int] = None
    metadata: Optional[Dict[str, Any]] = None
    created_at: datetime
    
    class Config:
        from_attributes = True

class DeploymentConfigBase(BaseModel):
    """部署配置基础模式"""
    name: str = Field(..., max_length=100, description="配置名称")
    environment: str = Field(..., max_length=50, description="环境名称")
    config_data: Dict[str, Any] = Field(..., description="配置数据")
    description: Optional[str] = Field(None, description="配置描述")

class DeploymentConfigCreate(DeploymentConfigBase):
    """创建部署配置模式"""
    is_active: bool = Field(True, description="是否激活")

class DeploymentConfig(DeploymentConfigBase):
    """部署配置响应模式"""
    id: int
    created_by: int
    is_active: bool
    created_at: datetime
    updated_at: Optional[datetime]
    
    class Config:
        from_attributes = True

class ReleaseStats(BaseModel):
    """发布统计模式"""
    total_releases: int
    successful_releases: int
    failed_releases: int
    rollback_count: int
    success_rate: float
    environment_stats: List[Dict[str, Any]]

class EnvironmentStat(BaseModel):
    """环境统计模式"""
    environment: str
    count: int